const ContextActionService = game.GetService('ContextActionService');
function initPress(button?: ImageButton | GuiObject,
    pressCall?: () => boolean, inputType?: Enum.UserInputType, key?: Enum.KeyCode,
    releasCall?: () => boolean) {
    const onPress = (pos?: Vector2[]) => {

        if (pressCall) pressCall();
    }
    const onRelease = (pos?: Vector2[]) => {

        if (releasCall) releasCall();
    }

    if (button) {

        if (button.IsA('GuiButton')) {
            let pressed = false
            button.MouseButton1Down.Connect((x, y) => {
                pressed = true
                onPress([new Vector2(x, y)])
            });
            button.MouseLeave.Connect((x, y) => {
                if (pressed) {
                    pressed = false;
                    onRelease([new Vector2(x, y)])
                }

            });
            button.MouseButton1Up.Connect((x, y) => {
                pressed = false;
                onRelease([new Vector2(x, y)])
            })
        }
        // button.TouchTap.Connect((pos) => {
        //     onPress(pos)
        // })
        // button.TouchLongPress.Connect((pos, state) => {

        //     if (state === Enum.UserInputState.End) {
        //         onRelease(pos)
        //     }

        // })
    }

    if (inputType) {
        ContextActionService.UnbindAction(inputType.Name + key || '')
        ContextActionService.BindAction(
            inputType.Name + key || '',
            (actionName: string, state: Enum.UserInputState, inputObject: InputObject) => {

                if (key && key !== inputObject.KeyCode) {
                    return
                }
                if (state === Enum.UserInputState.Begin) {
                    onPress()
                }
                if (state === Enum.UserInputState.End) {
                    onRelease()
                }
            },
            false, key || inputType)
    }
}
function initRelease(button?: ImageButton | GuiObject,
    pressCall?: () => boolean, inputType?: Enum.UserInputType, key?: Enum.KeyCode) {
    const onPress = (pos?: Vector2[]) => {
        // this.clearMoveOffset();
        if (pressCall) pressCall();
    }
    // this.clearPress();
    if (button) {
        // button.Active = false;
        // this.controllerbuttons.push(button);
        if (button.IsA('GuiButton')) {
            button.MouseButton1Up.Connect((x, y) => onPress([new Vector2(x, y)]));
            // button.MouseButton1Up.Connect(onRelease)
        }
        button.TouchLongPress.Connect((pos, state) => {
            // print('00000', pos)
            if (state === Enum.UserInputState.End) {
                onPress(pos)
            }
        })
    }

    if (inputType) {
        ContextActionService.UnbindAction(inputType.Name + key + '_End' || '')
        ContextActionService.BindAction(
            inputType.Name + key || '',
            (actionName: string, state: Enum.UserInputState, inputObject: InputObject) => {

                if (key && key !== inputObject.KeyCode) {
                    return
                }
                if (state === Enum.UserInputState.End) {
                    onPress()
                }
            },
            false, key || inputType)
    }
}
export {
    initPress,
    initRelease
}